﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MReviewerClient.Helpers
{
    public class CommonProvider
    {
        /// <summary>
        /// The instance of the class
        /// </summary>
        private static CommonProvider instance;

        /// <summary>
        /// The lock object to prevent multi process play the sound at the same time
        /// </summary>
        private static object syncLock = new object();

        /// <summary>
        /// The singleton pattern to guarantee only an object will be created
        /// </summary>
        public static CommonProvider Instance
        {
            get
            {
                lock (syncLock)
                {
                    if (instance == null)
                        instance = new CommonProvider();
                    return instance;
                }
            }
        }

        /// <summary>
        /// Find first child control with specific name.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public FrameworkElement GetTemplateChildByName(DependencyObject parent, string name)
        {
            int childnum = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childnum; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                if (child is FrameworkElement && ((FrameworkElement)child).Name == name)
                {
                    return child as FrameworkElement;
                }
                else
                {
                    var s = GetTemplateChildByName(child, name);
                    if (s != null)
                        return s;
                }
            }
            return null;
        }
    }
}
